package com.yunshipei.ui.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

public class SimpleSwipeRefreshLayout extends SwipeRefreshLayout {

    private View contacts;
    private View department;
    private ViewPager viewPager;

    public SimpleSwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public SimpleSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initColor();
    }

    private void initColor() {
        setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_blue_bright);
    }

    public void setViewGroup(View view) {
        int tag = (Integer) view.getTag();
        if (tag == 0) {
            contacts = view;
        } else {
            department = view;
        }
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    @Override
    public boolean canChildScrollUp() {
        if (viewPager != null && !isRefreshing()) {
            if (viewPager.getCurrentItem() == 0 && contacts != null && contacts instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) contacts;
                return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
            }
            if (viewPager.getCurrentItem() == 1 && department != null && department instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) department;
                return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
            }
        }
        return super.canChildScrollUp();
    }
}